package com.second.app.controller;


import com.second.app.entity.Example;
import com.second.app.service.IExampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 1542365144@qq.com
 * @since 2024-04-20
 */
@RestController
@RequestMapping("/example")
public class ExampleController {


    @Autowired
    private IExampleService exampleService;

    @PostMapping("update/example")
    public void updateExample() {

        Long id = 1L;

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(7, 8,
                5, TimeUnit.SECONDS, new LinkedBlockingQueue<>());


        //模拟5个线程同时查询并更新id为1的数据行
        List<Future<?>> futureTask = new ArrayList<>();
        for (int i = 1; i < 3; i++) {
            int finalI = i*10;
            Future<?> submit = threadPoolExecutor.submit(() -> {
                 exampleService.updateExampleWithManualLock(id, "我是最帅的：" + finalI);
            });
            futureTask.add(submit);
        }

        for (Future<?> future : futureTask) {
            try {
                System.out.println(future.get());
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            }
        }


    }

}
